package com.youyou.fusheng2.View.Activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.youyou.fusheng2.Moudle.FuShengCallBack;
import com.youyou.fusheng2.Moudle.OnClick2;
import com.youyou.fusheng2.Person.FuShengMoudle;
import com.youyou.fusheng2.Person.Test.HomeTedayTest;
import com.youyou.fusheng2.Person.Test.VdioUrlTest;
import com.youyou.fusheng2.Person.Test.XQTest;
import com.youyou.fusheng2.R;
import com.youyou.fusheng2.Utils.EveryUrl;
import com.youyou.fusheng2.View.Adapter.VdioPagerAdapter;
import com.youyou.fusheng2.View.Adapter.VdioUrlListAdapter;
import com.youyou.fusheng2.View.Adapter.VdioUrlListAdapter2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jzvd.JzvdStd;

/**
 * @作者: $浮生-qq:2567294605$
 * @时间: 2020/8/29  04:09
 * @简介: 此页面 实现了 点击事件接口
 * 回掉参数 一个String标记  一个item的下标
 */
public class playVdioActivity extends AppCompatActivity implements FuShengCallBack, OnClick2, View.OnClickListener {

    private JzvdStd vdio_play;
    private TextView vdio_name;
    private TextView vdio_juji;
    private RecyclerView vdio_liebiao;
    private TextView vdio_remen;
    private int anInt;  //其他页面传过来的 vid
    private TextView vdio_juji2;
    private RecyclerView vdio_liebiao2;
    private RecyclerView vdio_list;
    private String v_pic;
    private String v_name;
    private String body;
    private int v_note;
    private Map<String, String> mu38map;
    private Map<String, String> othermap;
    private List<Map<String, String>> Mu38list = new ArrayList<>();
    private List<Map<String, String>> Otherlist = new ArrayList<>();
    private MyHander myHander = new MyHander();
    private FuShengMoudle fuShengMoudle = new FuShengMoudle(this);
    private List<Map<String, String>> typeTestLists = new ArrayList<>();
    private Map<String, String> typeTestmap;
    private int i = 0; //默认的播放下标
    private ImageView pupu_image;
    private TextView pupu_text;
    private RecyclerView show_pupuwindow;


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setBar();
        }
        setContentView(R.layout.paly_vdio);
        anInt = getIntent().getBundleExtra("text").getInt("f");
        initView();
        GetShuju(anInt);
        GetTuiJian();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void setBar() {
        this.getWindow().setStatusBarColor(Color.parseColor("#3A3939"));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }


    private void GetTuiJian() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  04:07
         *  @简介: 获取底部推荐 九宫格 布局
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                fuShengMoudle.getUrlDate(EveryUrl.vdioTj, 2);
            }
        }).start();
    }

    private void setPlay(int i) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  04:06
         *  @简介: 默认播放集合的第0个url 当回掉函数执行的时候调用 根据回掉下标 重新设置播放资源
         */
        vdio_play.setUp(Mu38list.get(i).get("url"), v_name);
        Glide.with(this).load(v_pic).into(vdio_play.posterImageView);
        vdio_play.startVideo();//自动播放
    }

    private void initAdapter(List<Map<String, String>> list, int i) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  01:01
         *  @简介: 初始化适配器
         */
        if (i == 0) {
            setAdapter(vdio_liebiao, list);
        } else {
            setAdapter(vdio_liebiao2, list);
        }
    }

    private void setAdapter(RecyclerView vdio_liebiao2, List<Map<String, String>> list) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  03:56
         *  @简介: 播放页面播放列表
         */
        VdioUrlListAdapter vdioUrlListAdapter = new VdioUrlListAdapter(list, (OnClick2) this, 1);
        vdio_liebiao2.setAdapter(vdioUrlListAdapter);
        vdio_liebiao2.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        vdioUrlListAdapter.notifyDataSetChanged();
    }


    private void GetShuju(int anInt) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //视频URL
                fuShengMoudle.getUrlDate(EveryUrl.vdioURL + anInt, 0);
                //视频详情
                fuShengMoudle.getUrlDate(EveryUrl.vdioXQ + anInt, 1);
            }
        }).start();
    }


    @SuppressLint({"SetTextI18n", "CutPasteId"})
    private void initView() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  04:05
         *  @简介: 初始化
         */
        vdio_play = (JzvdStd) findViewById(R.id.vdio_play);
        vdio_list = findViewById(R.id.vdio_list);
        vdio_name = (TextView) findViewById(R.id.vdio_name);
        vdio_juji = (TextView) findViewById(R.id.vdio_juji);
        vdio_liebiao = (RecyclerView) findViewById(R.id.vdio_liebiao);
        vdio_remen = (TextView) findViewById(R.id.vdio_remen);
        vdio_juji2 = (TextView) findViewById(R.id.vdio_juji2);
        vdio_liebiao2 = (RecyclerView) findViewById(R.id.vdio_liebiao2);
        vdio_juji.setOnClickListener(this);
        vdio_juji2.setOnClickListener(this);
        vdio_juji.setText("·\tMU38剧集  ·  DISSME >");
        vdio_juji2.setText("·\tOTHER剧集 ·  DISSME >");
        vdio_remen.setText("热门推荐:");
        RelativeLayout ll = (RelativeLayout) findViewById(R.id.ll);
        JzvdStd vdioPlay = (JzvdStd) findViewById(R.id.vdio_play);
        TextView vdioName = (TextView) findViewById(R.id.vdio_name);
        TextView vdioJuji = (TextView) findViewById(R.id.vdio_juji);
        RecyclerView vdioLiebiao = (RecyclerView) findViewById(R.id.vdio_liebiao);
        TextView vdioJuji2 = (TextView) findViewById(R.id.vdio_juji2);
        RecyclerView vdioLiebiao2 = (RecyclerView) findViewById(R.id.vdio_liebiao2);
        TextView vdioRemen = (TextView) findViewById(R.id.vdio_remen);
        RecyclerView vdioList = (RecyclerView) findViewById(R.id.vdio_list);
    }

    @Override
    public void GetSuccess(String text, int mark) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  20:18
         *  @简介: 请求成功
         */
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void GetError(String text, int mark) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  20:18
         *  @简介: 请求失败
         */
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void listener1(int Flage, String Text) {

    }

    @Override
    public void listener1(int Flage, String Text, int Index) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  03:58
         *  @简介: 如果回掉数据等于 传归来的数据 进行相关操作
         */
        if (Flage == 1) {
            //点击底部item推荐的时候 清空剧集列表
            //根据回掉的下标重新渲染页面 切换资源播放
            Mu38list.clear();
            Otherlist.clear();
            GetShuju(Integer.parseInt(typeTestLists.get(Index).get("tid")));
        }
        if (Flage == 0) {
            //设置点击底部ITEM的下标获得播放链接
            setPlay(Index);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.vdio_juji:
                //显示PopupWindow
                setPupuWindow(view);
                setAdapter1(show_pupuwindow, Mu38list);
                break;
            case R.id.vdio_juji2:
                setPupuWindow(view);
                setAdapter1(show_pupuwindow, Otherlist);
                break;
        }
    }

    private void setPupuWindow(View view) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  21:13
         *  @简介: 根据当前的点击事件 显示pupowindow
         */
        @SuppressLint("InflateParams")
        View contentview = LayoutInflater.from(this).inflate(R.layout.pupuwindow, null, false);
        PopupWindow popupWindow = new PopupWindow(contentview, ViewGroup.LayoutParams.MATCH_PARENT,
                (int) ((getResources().getDisplayMetrics().heightPixels / 3) * 1.5));
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        initPupuwindow(contentview);
    }

    @SuppressLint("SetTextI18n")
    private void initPupuwindow(View contentview) {
        pupu_image = contentview.findViewById(R.id.pupu_image);
        pupu_text = contentview.findViewById(R.id.pupu_text);
        show_pupuwindow = contentview.findViewById(R.id.show_pupuwindow);
        Glide.with(this).load(v_pic).into(pupu_image);
        pupu_text.setText(v_name + "\n\n" + body);
    }

    private void setAdapter1(RecyclerView show_pupuwindow, List<Map<String, String>> mu38list) {
        VdioUrlListAdapter2 vdioUrlListAdapter2 = new VdioUrlListAdapter2(mu38list, (OnClick2) this, 0);
        show_pupuwindow.setAdapter(vdioUrlListAdapter2);
        show_pupuwindow.setLayoutManager(new GridLayoutManager(this, 4));
        vdioUrlListAdapter2.notifyDataSetChanged();
    }

    public class MyHander extends Handler {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    //解析播放地址 mu38 和 other
                    jiexiVdioUrl(msg.obj.toString());
                    initAdapter(Mu38list, 0);
                    initAdapter(Otherlist, 1);
                    break;
                case 1:
                    //解析底部详情
                    jiexi(msg.obj.toString());
                    setPlay(i);
                    break;
                case 2:
                    //解析最底部九宫格 数据
                    jiexiTJ(msg.obj.toString());
                    initViewPager();
                    break;
            }
        }
    }

    private void jiexiTJ(String toString) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  04:02
         *  @简介: 解析推荐
         */
        Gson gson = new Gson();
        HomeTedayTest homeTedayTest = gson.fromJson(toString, HomeTedayTest.class);
        if (homeTedayTest.getCode() == 0) {
            List<HomeTedayTest.DataBean> data = homeTedayTest.getData();
            for (int i = 0; i < data.size(); i++) {
                typeTestmap = new HashMap<>();
                typeTestmap.put("pic", data.get(i).getV_pic());
                typeTestmap.put("tid", String.valueOf(data.get(i).getV_id()));
                typeTestmap.put("tname", String.valueOf(data.get(i).getV_name()));
                typeTestLists.add(typeTestmap);
            }
        }
    }

    @SuppressLint("ResourceAsColor")
    private void initViewPager() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  02:17
         *  @简介: 设置底部九宫格数据
         */
        VdioPagerAdapter souSuoPagerAdapter = new VdioPagerAdapter(typeTestLists, (OnClick2) this);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        vdio_list.setAdapter(souSuoPagerAdapter);
        vdio_list.setLayoutManager(layoutManager);
        souSuoPagerAdapter.notifyDataSetChanged();
    }

    private void jiexiVdioUrl(String toString) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  00:55
         *  @简介: 解析视频播放链接
         */
        Gson gson = new Gson();
        VdioUrlTest vdioUrlTest = gson.fromJson(toString, VdioUrlTest.class);
        VdioUrlTest.DataBean data = vdioUrlTest.getData();
        List<VdioUrlTest.DataBean.M3u8ListBean> m3u8List = data.getM3u8List();
        for (int i = 0; i < m3u8List.size(); i++) {
            mu38map = new HashMap<>();
            mu38map.put("name", m3u8List.get(i).getName());
            mu38map.put("url", m3u8List.get(i).getUrl());
            Mu38list.add(mu38map);
        }
        List<VdioUrlTest.DataBean.OtherListBean> otherList = data.getOtherList();
        for (int i = 0; i < otherList.size(); i++) {
            othermap = new HashMap<>();
            othermap.put("name", otherList.get(i).getName());
            othermap.put("url", otherList.get(i).getUrl());
            Otherlist.add(othermap);
        }
    }

    @SuppressLint("SetTextI18n")
    private void jiexi(String s) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/28  20:42
         *  @简介: 解析数据详情
         */
        Gson gson = new Gson();
        XQTest xqTest = gson.fromJson(s, XQTest.class);
        v_name = xqTest.getData().get(0).getV_name();
        v_pic = xqTest.getData().get(0).getV_pic();
        body = xqTest.getData().get(0).getBody();
        v_note = xqTest.getData().get(0).getV_score();
        vdio_name.setText("影名:\t" + v_name + " · " + "全集:\t" + ((Mu38list.isEmpty()) ? 0 : Mu38list.size()) + "集\n资源加载失败时，请切换播放源！\n评分：" + v_note + "分");
    }


    @Override
    public void onBackPressed() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  04:03
         *  @简介: 设置饺子播放器的返回点击事件
         */
        if (JzvdStd.backPress()) {
            finish();
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  04:04
         *  @简介: 当activity为不可见状态 暂停播放
         */
        JzvdStd.releaseAllVideos();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  04:03
         *  @简介: activity销毁清理资源
         */
        super.onDestroy();
        if (Mu38list != null) {
            Mu38list.clear();
            Otherlist.clear();
            myHander = null;
        }
    }

}